package com.example.curriculum;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TableRow;
import android.widget.Toast;

public class setActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        TableRow tableRow = findViewById(R.id.more_page_row4);
        TableRow tableRow1 = findViewById(R.id.more_page_row5);
        tableRow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items5 = new String[]{"允许通知", "后台播放", "读取存储空间", "获取联系人列表"};//创建item
                final boolean[] booleans = {true, true, false, false};
                AlertDialog alertDialog5 = new AlertDialog.Builder(setActivity.this)
                        .setTitle("选择您需要的权限")
                        .setIcon(R.mipmap.ic_launcher)
                        .setMultiChoiceItems(items5, booleans, new DialogInterface.OnMultiChoiceClickListener() {//创建多选框
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                booleans[i] = b;
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                StringBuilder stringBuilder = new StringBuilder();
                                for (int a = 0; a < booleans.length; a++) {
                                    if (booleans[a]) {
                                        stringBuilder.append(items5[a] + " ");
                                    }
                                }
                                Toast.makeText(setActivity.this, "权限开启成功" + stringBuilder.toString(), Toast.LENGTH_SHORT).show();
                            }
                        })

                        .setNegativeButton("跳转到应用权限页面", new DialogInterface.OnClickListener() {//添加取消
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent localIntent = new Intent();
                                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                if (Build.VERSION.SDK_INT >= 9) {
                                    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                    localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                                } else if (Build.VERSION.SDK_INT <= 8) {
                                    localIntent.setAction(Intent.ACTION_VIEW);
                                    localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                                    localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
                                }
                                startActivity(localIntent);
                            }
                        })
                        .create();
                alertDialog5.show();
            }
        });
        tableRow1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("http://www.baidu.com");
                Intent it = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(it);
            }
        });
    }
}